var express = require('express');
var xlsx = require('node-xlsx');
var uuid = require('node-uuid');
var Pro  = require('./../sql/collection/pros');
var sql  = require('./../sql');
var router = express.Router();
var filestr = 'E:/js资源/千锋/第三阶段/vue_frontend/数据/shop.xlsx';

router.get('/',function(req,res,next){
    let{pageCode,limitNum} = req.query;
    pageCode = pageCode * 1 || 0;
    limitNum = limitNum * 1 || 10;

    sql.paging(Pro,{},{_id:0},limitNum,pageCode).then(data =>{
        res.send({
            code:'200',
            success:'查询列表成功',
            length:data.length,
            data:data
        })
    })
});

router.get('/detail',(req,res,next) =>{
    let{proid} = req.query
    sql.find(Pro,{proid},{_id:0}).then(data =>{
        res.send({
            code:'200',
            message:'查询该数据成功',
            data:data[0]
        })
    })
})


router.get('/type',(req,res,next) => {
    let {type} = req.query
    type = type ||'type'
    sql.distinct(Pro,type).then(data =>{
        res.send({
            code:'200',
            success:'获取类型成功',
            data:data
        })
    })
});

router.get('/import',(req,res,next) =>{
    let obj = xlsx.parse(filestr)[0].data
    let arr = []
    obj.map((item,index) =>{
        if(index !== 0){
            arr.push({
                proid:"pro_" + uuid.v1(),
                type:item[0],
                brand:item[1],
                barndimg:item[2],
                proname:item[3],
                price:item[4],
                flag:item[5],
                proimg:item[6],
                note:item[7],
                postage:item[8]
            })
        }
    })
    sql.insert(Pro,arr).then(()=>{
        res.send(arr)
    })
})
// 获取分类类型对应的品牌
router.get('/category', (req, res, next) => {
    let { type } = req.query
    sql.find(Pro, { type }, {_id: 0, brand:1, barndimg: 1}).then(data => {
      // 数组去重 https://www.cnblogs.com/le220/p/9130656.html
      let obj = {}
      // 利用reduce方法遍历数组,reduce第一个参数是遍历需要执行的函数，第二个参数是item的初始值
      data = data.reduce((item, next) => {
        obj[next.brand] ? '' : obj[next.brand] = true && item.push(next)
        return item
      }, [])
      res.send({
        code: '200',
        message: '获取分类类型列表',
        data: data
      })
    })
  })
  
  // 获取品牌类型对应的产品
  router.get('/brandcategory', (req, res, next) => {
    let { brand } = req.query
    sql.find(Pro, { brand: brand }, {_id: 0}).then(data => {
      
      res.send({
        code: '200',
        message: '获取品牌分类列表',
        data: data
      })
    })
  })
  
  // 搜索
  router.get('/search', (req, res, next) => {
    let { text } = req.query
    sql.find(Pro, { proname: eval('/' + text + '/') }, {_id: 0}).then(data => {
      
      res.send({
        code: '200',
        message: '搜索列表',
        data: data
      })
    })
  })
  

module.exports = router;






